package com.lframework.xingyun.basedata.mappers;

import com.lframework.starter.web.core.mapper.BaseMapper;
import com.lframework.xingyun.basedata.dto.make.MakeMethodMaterialDto;
import com.lframework.xingyun.basedata.entity.*;

import java.util.List;

/**
 * <p>
 * 制作方法与材料关系表 Mapper 接口
 * </p>
 *
 */
public interface MakeMethodMaterialMapper extends BaseMapper<MakeMethodMaterial> {

    /**
     * 根据做法id和材料类型查询材料列表
     * @param makeId
     * @return
     */
    default List<MakeMethodMaterialDto> selectDtoByMakeId(String makeId) {
        return defaultJoinWrapper()
                .selectAll(MakeMethodMaterial.class)
                .selectAs(Product::getId, MakeMethodMaterialDto::getProductId)
                .selectAs(Product::getName, MakeMethodMaterialDto::getProductName)
                .selectAs(Product::getSpec, MakeMethodMaterialDto::getSpec)
                .selectAs(Unit::getUnitName, MakeMethodMaterialDto::getUnitName)
                .leftJoin(Product.class, on -> on
                        .eq(Product::getCode, MakeMethodMaterial::getCode)
                        .eq(Product::getSkuCode, MakeMethodMaterial::getSkuCode)
                        .isNull(Product::getShopId)
                )
                .leftJoin(Unit.class, Unit::getId, MakeMethodMaterial::getConsumeUnitId)
                .eq(MakeMethodMaterial::getMakeId, makeId)
                .orderByAsc(MakeMethodMaterial::getOrderNo)
                .list(MakeMethodMaterialDto.class);
    }

    default List<MakeMethodMaterial> selectByCodeAndSkuCode(String code, String skuCode) {
        return defaultJoinWrapper()
                .selectAll(MakeMethodMaterial.class)
                .leftJoin(MakeMethod.class, MakeMethod::getId, MakeMethodMaterial::getMakeId)
                .leftJoin(ProductMake.class, ProductMake::getMakeId, MakeMethod::getId)
                .eq(ProductMake::getCode, code)
                .eq(ProductMake::getSkuCode, skuCode)
                .list();
    }
}

